package com.mes.eventhandler;

import com.mes.quality.data.EmesqInspectionEDItem;
import com.mes.quality.data.EmesqInspectionEnumData;
import org.codehaus.jettison.json.JSONObject;
import org.hibernate.Query;
import org.openbravo.base.exception.OBException;
import org.openbravo.base.model.Entity;
import org.openbravo.base.model.ModelProvider;
import org.openbravo.client.kernel.event.EntityDeleteEvent;
import org.openbravo.client.kernel.event.EntityNewEvent;
import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
import org.openbravo.client.kernel.event.EntityUpdateEvent;
import org.openbravo.dal.core.OBContext;
import org.openbravo.dal.service.OBDal;
import org.openbravo.database.ConnectionProvider;
import org.openbravo.erpCommon.utility.Utility;
import org.openbravo.service.db.DalConnectionProvider;
import javax.enterprise.event.Observes;
import java.util.List;

public class InspectionEDItemEventHandler extends EntityPersistenceEventObserver {
  private static Entity[] entities = { ModelProvider.getInstance().getEntity(EmesqInspectionEDItem.ENTITY_NAME) };
  // protected Logger logger = Logger.getLogger(this.getClass());

  @Override
  protected Entity[] getObservedEntities() {
    return entities;
  }

  public void onUpdate(@Observes EntityUpdateEvent event) throws Throwable {
    if (!isValidEvent(event)) {
      return;
    }
    final EmesqInspectionEDItem emesqInspectionEDItem = (EmesqInspectionEDItem) event.getTargetInstance();

    EmesqInspectionEnumData emesqnspectionEnumData = emesqInspectionEDItem.getEmesqInspectionEnumData();
    String InspCode = emesqnspectionEnumData.getCode();
    String enumData_Result = emesqInspectionEDItem.getResult();
    String id = emesqInspectionEDItem.getId();

    // 区分工序唯一
    String hql = "select id from Emesq_Inspection_ED_Item eiet where eiet.emesqInspectionEnumData.inspectionEnumdataCode ='" + InspCode
        + "' and eiet.enumdataResult ='" + enumData_Result + "' and eiet.id != '" + id + "'";
    Query query = OBDal.getInstance().getSession().createQuery(hql);
    List<JSONObject> list = query.list();

    if (!list.isEmpty()) {
      String language = OBContext.getOBContext().getLanguage().getLanguage();
      ConnectionProvider conn = new DalConnectionProvider(false);
      throw new OBException(Utility.messageBD(conn, "在同一编码的选检项目中，选检的结果不应该相同，请仔细检查。", language));
    }

  }

  public void onSave(@Observes EntityNewEvent event) {
    if (!isValidEvent(event)) {
      return;
    }
    final EmesqInspectionEDItem emesqInspectionEDItem = (EmesqInspectionEDItem) event.getTargetInstance();

    EmesqInspectionEnumData emesqnspectionEnumData = emesqInspectionEDItem.getEmesqInspectionEnumData();
    String InspCode = emesqnspectionEnumData.getCode();
    String enumData_Result = emesqInspectionEDItem.getResult();
    String id = emesqInspectionEDItem.getId();

    // 区分唯一
    String hql = "select eiet.id from Emesq_Inspection_ED_Item eiet where eiet.emesqInspectionEnumData.inspectionEnumdataCode ='" + InspCode
        + "' and eiet.enumdataResult ='" + enumData_Result + "' and eiet.id != '" + id + "'";
    try {
    	List<Object> list = OBDal.getInstance().getSession().createQuery(hql).list();
   
    if (!list.isEmpty()) {
      String language = OBContext.getOBContext().getLanguage().getLanguage();
      ConnectionProvider conn = new DalConnectionProvider(false);
      throw new OBException(Utility.messageBD(conn, "在同一编码的选检项目中，选检的结果不应该相同，请仔细检查。", language));
    }
    
    } catch (Exception e) {
		// TODO: handle exception
    	e.printStackTrace();
	}

  }

  public void onDelete(@Observes EntityDeleteEvent event) {
    if (!isValidEvent(event)) {
      return;
    }
    // logger.info("Greeting " + event.getTargetInstance().getId() + " is being deleted");
  }
}
